// 比较版本号

package Leetcode;

class Solution165_1 {
    public int compareVersion(String version1, String version2) {
        String[] array1 = version1.split("\\.");
        String[] array2 = version2.split("\\.");
        // System.out.println(Arrays.toString(array1));
        // System.out.println(Arrays.toString(array2));

        for (int i = 0; i < array1.length; i++) {
            array1[i] = array1[i].replaceAll("0+([\\d^0]+)", "$1");
        }
        for (int i = 0; i < array2.length; i++) {
            array2[i] = array2[i].replaceAll("0+([\\d^0]+)", "$1");
        }
        // System.out.println(Arrays.toString(array1));
        // System.out.println(Arrays.toString(array2));

        int maxLength = Math.max(array1.length, array2.length);
        for (int i = 0; i < maxLength; i++) {
            // 后续不足的用0替代
            String s1 = array1.length <= i ? "0" : array1[i];
            String s2 = array2.length <= i ? "0" : array2[i];
            // 位数不同时，高位数越大
            if (s1.length() != s2.length())
                return s1.length() - s2.length() > 0 ? 1 : -1;
            // 位数相同时比较数字的值
            if (s1.compareTo(s2) == 0)
                continue;
            else
                return s1.compareTo(s2) > 0 ? 1 : -1;
        }
        return 0;
    }

    public static void main(String[] args) {
        String s1 = "1.033";
        String s2 = "1.010";
        Solution165_1 s165_1 = new Solution165_1();
        System.out.println(s165_1.compareVersion(s1, s2));
    }
}

class Solution165_2 {
    public int compareVersion(String version1, String version2) {
        String[] array1 = version1.split("\\.");
        String[] array2 = version2.split("\\.");

        for (int i = 0; i < array1.length || i < array2.length; i++) {
            int index1 = i >= array1.length ? 0 : Integer.parseInt(array1[i]);
            int index2 = i >= array2.length ? 0 : Integer.parseInt(array2[i]);

            if (index1 == index2)
                continue;
            else
                return index1 - index2 > 0 ? 1 : -1;
        }
        return 0;
    }
}